#This is another linear regression example source( "../gnrnd4.R") # generate and inspect the values that we will use gnrnd4(6271641306,4161000704, 30000100) L1 L2 plot( L1, L2 ) # make a first, rough, scatter plot #refine that plot a bit plot( L1, L2, main="Our Linear Regression Example", xlim=c(-15,25), ylim=c(-15,45), xaxp=c(-15,25,8), yaxp=c(-15,45,12), las=1, cex.axis=0.75, xlab="L1 -- the x value", ylab="L2 -- the y value", pch=20, col="darkgreen") abline( h=seq(-15,45,5), v=seq(-15,25,5), lty="dotted", col="red") abline( h=0, v=0, col="darkred") # look at the correlation coefficient cor( L1, L2 ) # generate the linear regression model lm_hold <- lm( L2 ~ L1 ) lm_hold # look at the linear regression coefficients abline( lm_hold, col="blue") #plot the regression line # find all of the expected values expected <- 26.224 + (-1.636)*L1 expected #look at those values # now add the expected values to the plot points( L1, expected, pch=6, col="darkorange") # now, compute all of the residual values resid_vals <- L2 - expected resid_vals # we did not have to do that because we could # have just pulled the 'true' residual values # from the model residuals( lm_hold) # the difference is due to the fact that we used # the output from our linear model as the coefficients # for the regression equation. A more precise pair of # values could be pulled from the linear model coeffs <- coefficients( lm_hold) coeffs # and we could separate those values, and use # as.numeric() to remove the label intercept <- as.numeric(coeffs[1]) slope <- as.numeric(coeffs[2]) intercept slope # then, just as an extra display, if we want to sort # the values, according to the x-values we could # put them into a data frame comb <- data.frame(L1,L2,expected) comborder<-comb[order(L1),] L1sort<-comborder$L1 L2sort<-comborder$L2 expectedsort <- comborder$expected L1sort L2sort expectedsort